package cn.laowen.org.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.laowen.basic.exception.BusinessException;
import cn.laowen.basic.util.ExcelUtil;
import cn.laowen.basic.vo.ShopVo;
import cn.laowen.org.domain.ShopAuditLog;
import cn.laowen.org.mapper.ShopMapper;
import cn.laowen.org.service.IShopService;
import cn.laowen.org.domain.Shop;
import cn.laowen.org.query.ShopQuery;
import cn.laowen.basic.util.AjaxResult;
import cn.laowen.basic.util.PageList;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    @Autowired
    public ShopMapper shopMapper;


    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();

        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 审核通过
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();

        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }


    /**
     * 驳回
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();

        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 激活店铺
     * @param id
     * @param response
     * @return
     */
    @GetMapping("/activation/{id}")
    public void activation(@PathVariable("id")Long id,HttpServletResponse response) throws IOException {
        Shop shop = shopMapper.loadById(id);
        shop.setState(4);
        shopMapper.update(shop);
        response.sendRedirect("http://localhost:8081/#/login");
        System.out.println("店铺:"+shop.getId()+"激活成功^.^");
    }

    /**
     * excle导出
     * @param response
     */
    @GetMapping("/export/excle")
    public void export(HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xls",response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 导入excle
     * @param file
     */
    @PostMapping("/import/excel")
    public void importExcle(@RequestPart MultipartFile file){
        try {
            //导入数据
            List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
            System.out.println("导入的数据条数:"+shops.size());
            shops.forEach(System.out::println);

            //批量新增到shop表
            shopService.bulkSave(shops);
            System.out.println("批量新增完成^.^");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 不同店铺审核状态数量
     * @return
     */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        List<ShopVo> voList = shopService.queryEcharts();//查出不同店铺的状态数量
        List<String> states = voList.stream().map(ShopVo::getState).collect(Collectors.toList());//获取list中的state封装成一个list
        List<Integer> nums = voList.stream().map(ShopVo::getNum).collect(Collectors.toList());//获取list中的num封装成一个list
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("x",states);//设置到map,x为审核状态
        map.put("y",nums);//设置到map,y为不同店铺审核状态的数量
        return map;
    }

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult batch(@RequestBody List<Long> ids){
        try {
            shopService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("耶,原来是系统瓦了,正在殴打程序员!!!");
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }
}
